//
//  ClassificationViewController.m
//  YL8G
//
//  Created by Liccon Chang on 12-7-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ClassificationViewController.h"
#import "UIButton+Custom.h"
#import "HomeViewController.h"

@interface ClassificationViewController(Private)

- (UIButton*)buttonWithOrigin:(CGPoint)origin
                  normalImage:(UIImage*)aImage
            hightlightedImage:(UIImage*)aHightlightedImage
                selectedImage:(UIImage*)aSelectedImage
                          tag:(int)tag
                      caption:(NSString*)caption;
@end

@implementation ClassificationViewController

#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
    [super loadView];
    self.navigationItem.title=@"分类";
    int x=4;
    int y=4;
    UIButton* classBtn=[self buttonWithOrigin:CGPointMake(x, y)
                                  normalImage:[UIImage imageNamed:@"class1.png"]
                            hightlightedImage:nil
                                selectedImage:nil
                                          tag:EBROKENEWS
                                      caption:@"情感文学"];
    [self.view addSubview:classBtn];
    
    x=162;
    y=4;
    classBtn=[self buttonWithOrigin:CGPointMake(x, y)
                        normalImage:[UIImage imageNamed:@"class2.png"]
                  hightlightedImage:nil
                      selectedImage:nil
                                tag:EBAGAJW
                            caption:@"恋爱感悟"];
    [self.view addSubview:classBtn];
    
    x=4;
    y=82;
    classBtn=[self buttonWithOrigin:CGPointMake(x, y)
                        normalImage:[UIImage imageNamed:@"class3.png"]
                  hightlightedImage:nil
                      selectedImage:nil
                                tag:EFCDP
                            caption:@"婚姻家庭"];
    [self.view addSubview:classBtn];
    
    x=162;
    y=82;
    classBtn=[self buttonWithOrigin:CGPointMake(x, y)
                        normalImage:[UIImage imageNamed:@"class4.png"]
                  hightlightedImage:nil
                      selectedImage:nil
                                tag:EZSFS
                            caption:@"单身男女"];
    [self.view addSubview:classBtn];
    
    x=4;
    y=160;
    classBtn=[self buttonWithOrigin:CGPointMake(x, y)
                        normalImage:[UIImage imageNamed:@"class5.png"]
                  hightlightedImage:nil
                      selectedImage:nil
                                tag:EDTTS
                            caption:@"婆媳关系"];
    [self.view addSubview:classBtn];
    
    x=162;
    y=160;
    classBtn=[self buttonWithOrigin:CGPointMake(x, y)
                        normalImage:[UIImage imageNamed:@"class6.png"]
                  hightlightedImage:nil
                      selectedImage:nil
                                tag:EZSFS
                            caption:@"话题调查"];
    [self.view addSubview:classBtn];
    
    x=4;
    y=238;
    classBtn=[self buttonWithOrigin:CGPointMake(x, y)
                        normalImage:[UIImage imageNamed:@"class7.png"]
                  hightlightedImage:nil
                      selectedImage:nil
                                tag:EDTTS
                            caption:@"转载空间"];
    [self.view addSubview:classBtn];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* closeBaoku = [defaults objectForKey:@"closeRecommend"];
    if (closeBaoku ==nil || [closeBaoku isEqualToString:@"0"])
    {
        MobiSageRecommendView* recmdView = [[MobiSageRecommendView alloc] initWithDelegate:self andImg:nil];
        recmdView.frame=CGRectMake(320-80, (44-recmdView.frame.size.height)/2, recmdView.frame.size.width, recmdView.frame.size.height);
        [self.navigationController.navigationBar addSubview:recmdView];
        [recmdView release];
//        UIBarButtonItem* rightBar=[[UIBarButtonItem alloc] initWithCustomView:recmdView];
//        self.navigationItem.rightBarButtonItem=rightBar;
//        [rightBar release];
    }
}

- (UIButton*)buttonWithOrigin:(CGPoint)origin
                  normalImage:(UIImage*)aImage
            hightlightedImage:(UIImage*)aHightlightedImage
                selectedImage:(UIImage*)aSelectedImage
                          tag:(int)tag
                      caption:(NSString*)caption
{
    UIButton* retBtn=[UIButton buttonWithOrigin:origin
                                     normalImage:aImage
                               hightlightedImage:aHightlightedImage
                                   selectedImage:aSelectedImage
                                    asBackground:FALSE];
    [retBtn addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside];
    retBtn.tag=tag;
    [retBtn setTitle:caption forState:UIControlStateNormal];
    
    UILabel* captionLabel=[[UILabel alloc] initWithFrame:CGRectMake(5, 45, 140, 30)];
    captionLabel.font=[UIFont systemFontOfSize:18];
    captionLabel.text=caption;
    captionLabel.backgroundColor=[UIColor clearColor];
    [retBtn addSubview:captionLabel];
    [captionLabel release];
    return  retBtn;
}

- (void)btnPress:(UIButton*)sender
{
    HomeViewController* homeCtrl = [[HomeViewController alloc] init];
    homeCtrl.categoryId=@"feeling";
    homeCtrl.categoryName=@"情感专区";
    homeCtrl.isRecommend=FALSE;
    homeCtrl.navigationItem.title=[sender titleForState:UIControlStateNormal];
    homeCtrl.subItem=[sender titleForState:UIControlStateNormal];
    homeCtrl.hidesBottomBarWhenPushed=TRUE;
    [self.navigationController pushViewController:homeCtrl animated:TRUE];
    [homeCtrl release];
}

#pragma mark -
#pragma mark AdSageRecommendDelegate
- (UIViewController *)viewControllerForPresentingModalView
{
    return self.tabBarController;
}

- (void) adSageWillOpenRecommendModalView
{
    
}

- (void) adSageFailToOpenRecommendModalView
{
    NSLog(@"推荐弹出失败");
}

- (void) adSageDidCloseRecommendModalView
{
    NSLog(@"推荐关闭");
}

#pragma mark - View lifecycle
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{    
    [super dealloc];
}

@end
